package com.ch9;

import java.util.Random;

/**
 * 求一个数的十六进制中1的个数
 * @author Linxiaozhi
 *
 */
public class CountBit {

	static void count(int val) {
		int counter = 0;
		while (val != 0) {
			val = val & (val - 1);
			counter++;
		}
		System.out.println("counter:" + counter);
	}

	public static void main(String[] args) {
		Random rand = new Random(System.currentTimeMillis());
		for (int i = 0; i < 10; i++) {
			int val = rand.nextInt(1000000);
			System.out.print(Integer.toBinaryString(val) + "     "+Integer.bitCount(val)+"       ");
			count(val);
		}
	}
}
